<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>客户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/dtree.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/font/dtreefont.css" media="all" />
</head>
<body class="childrenBody">
<!-- 查询条件开始-->

<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form" >
            <div class="layui-form-item">
                <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" name="customername" id="customername" class="layui-input searchVal" placeholder="请输入客户姓名" />
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" name="telephone" id="telephone" class="layui-input searchVal" placeholder="请输入客户电话" />
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" name="linkman" id="linkman" class="layui-input searchVal" placeholder="请输入客户联系人" />
                        </div>
                </div>
                <button type="button" class="layui-btn" lay-submit lay-filter="doSearch"><span class="layui-icon layui-icon-search"></span>查询</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </form>
    </blockquote>
</form>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="customerTable" lay-filter="customerTable"></table>
    <!-- 工具栏 -->
    <div id="customerToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>添加角色</button>
        <button type="button" lay-event="batchDelete" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-delete"></span>批量删除</button>
    </div>

    <div id="customerRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-edit"></span>更新</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger">
            <span class="layui-icon layui-icon-delete"></span>删除
        </button>
        <button type="button" lay-event="selectRolePermission" class="layui-btn layui-btn-sm">
            <span class="layui-icon layui-icon-about"></span>分配权限
        </button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div  style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form class="layui-form" style="width:80%;margin-top: 20px;" id="dataFrm" lay-filter="dataFrm">
        <!-- 隐藏域，保存角色id -->
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户名称</label>
                <div class="layui-input-block">
                    <input type="text" name = "customername" class="layui-input" autocomplete="off"  lay-verify="required" placeholder="请输入客户名称">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">公司电话</label>
                <div class="layui-input-block">
                    <input type="text" name = "telephone" class="layui-input" autocomplete="off"  lay-verify="required" placeholder="请输入公司电话">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">联系人</label>
                <div class="layui-input-block">
                    <input type="text" name = "linkman" class="layui-input" autocomplete="off"  lay-verify="required" placeholder="请输入联系人">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">联系人</label>
                <div class="layui-input-block">
                    <input type="text" name = "phone" class="layui-input" autocomplete="off"  lay-verify="required" placeholder="请输入联系人电话">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">邮编</label>
                <div class="layui-input-block">
                    <input type="text" name = "zipcode" class="layui-input" autocomplete="off"  lay-verify="required" placeholder="请输入客户邮编">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name = "email" class="layui-input" autocomplete="off"  lay-verify="required" placeholder="请输入客户邮箱">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">开户银行</label>
                <div class="layui-input-block">
                    <input type="text" name = "bank" class="layui-input" autocomplete="off"  lay-verify="required" placeholder="请输入开户银行">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">传真</label>
                <div class="layui-input-block">
                    <input type="text" name = "fax" class="layui-input" autocomplete="off"  lay-verify="required" placeholder="请输入传真">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input type="text" name = "account" class="layui-input" autocomplete="off"  lay-verify="required" placeholder="请输入客户账号">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">公司地址</label>
                <div class="layui-input-block">
                    <input type="text" name = "address" class="layui-input" autocomplete="off"  lay-verify="required" placeholder="请输入公司地址">
                </div>
            </div>
        </div>
        <div class="layui-form-item ">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit"><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->

<!-- 分配权限的弹出层 开始 -->
<div style="display: none;" id="selectRolePermissionDiv">
    <ul id="permissionTree" class="dtree" data-id="0"></ul>
</div>
<!-- 分配权限的弹出层 结束-->


<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['jquery','form','table','layer'],function () {
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        //渲染数据
        var tableIns = table.render({
            elem: '#customerTable'
            ,url:'/bus/customer/customerlist'
            ,toolbar:'#customerToolBar'
            ,title:"客户数据表"
            ,height: 'full-180'
            ,page: true
            ,cols: [ [
                {type:'checkbox',align:'left'}
                ,{field:'id',title:'ID',align: 'center', width:60}
                ,{field:'customername',title:'客户名称',align: 'center', width:120}
                ,{field:'zipcode',title:'邮编',align: 'center', width:100}
                ,{field:'address',title:'客户地址',align: 'center', width:150}
                ,{field:'telephone',title:'客户电话',align: 'center', width:150}
                ,{field:'linkman',title:'联系人',align: 'center', width:100}
                ,{field:'phone',title:'联系人电话',align: 'center', width:150}
                ,{field:'bank',title:'开户行',align: 'center', width:120}
                ,{field:'account',title:'账号',align: 'center', width:180}
                ,{field:'email',title:'邮箱',align: 'center', width:150}
                ,{field:'fax',title:'传真',align: 'center', width:120}
                ,{field:'right',title:'操作',align: 'center',toolbar:"#customerRowBar",width: 300}

            ] ]
            ,done:function (res,curr,count) {
                //非第一页，当前页无数据
                if(curr>1 && res.data.length == 0){
                    //当前页码-1
                    var pageValue = curr - 1;
                    tableIns.reload({
                        page:{curr:pageValue}   //修改页码
                    });
                }
            }
        });
        //模糊查询
        form.on("submit(doSearch)",function (data) {
            tableIns.reload({
                where:data.field,
                page:{
                    curr:1
                }
            });
        })
        //监听头部工具栏事件    form 和table 你有毒
        table.on("toolbar(customerTable)",function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddcustomerWindow();
                    break;
                case 'batchDelete':
                    batchDelete();
                    break;
            }
        });
        //弹出层索引，提交路径
        var mainIndex,url;
        //打开添加角色
        function openAddcustomerWindow() {
            mainIndex = layer.open({
                type:1,
                title:"添加客户",
                area:["800px","500px"],
                content:$("#addOrUpdateDiv"),//引用的内容窗口
                success:function () {
                    //清空表单数据
                    $("#dataFrm")[0].reset();
                    //添加的提交请求
                    url = "/bus/customer/addCustomer";
                }
            });
        }
        //监听表单提交按钮（添加菜单和修改菜单）
        form.on("submit(doSubmit)",function (data) {
            $.post(url,data.field,function(result){
                if(result.success){
                    //刷新数据表格
                    tableIns.reload();
                }
                //显示提示信息
                layer.msg(result.message);
                //关闭当前窗口
                layer.close(mainIndex);
            },"json");

            return false;
        });

        //批量删除函数
        //批量删除，如何判断
        function batchDelete(){
            //获取选中行
            var checkStatus = table.checkStatus('customerTable');

            //获取选中的数量
            var length = checkStatus.data.length;
            //判断长度是否大于0
            if(length>0){
                //提示是否删除
                layer.confirm("确定要删除这些数据吗？",{icon: 3,title: '提示'},function (index) {
                    //先获取数据
                    var checkData = checkStatus.data;
                    //再获取所选数据中行id
                    var idArr = [];    //声明数组这么神奇的吗
                    for(var i = 0; i < length; i++){
                        idArr.push(checkData[i].id);
                    }
                    //id数组
                    var ids = idArr.join(",");
                    //发送删除的请求
                    $.post("/bus/customer/batchDelete",{"ids":ids},function (result) {
                        //判断时候成功
                        if(result.success){
                            //成功后则刷新表格

                            tableIns.reload();
                        }
                        lay.msg(result.message);

                    },"json");

                    layer.close(index);
                });
            }else{
                //用户小儿没有选中行，只有不会操作的用户，没有技术不关的码农,皮一下很开心
                layer.msg('请选择要删除的行！',{icon: 0});
            }
        }

        //监听行工具栏事件
        table.on("tool(customerTable)",function (obj) {
            switch (obj.event) {
                //更新客户
                case 'update':
                    openUpdateCustomerWindow(obj.data);
                    break;
                case 'delete':
                    //删除客户
                    deleteCustomer(obj.data);
                    break;

            }
        });

        //修改角色
        function openUpdateCustomerWindow(data) {
            mainIndex = layer.open({
                type:1,
                title:"更新客户",
                area:["800px","500px"],
                content:$("#addOrUpdateDiv"),//引用的内容窗口
                success:function () {
                    //表单数据回显
                    form.val("dataFrm",data);
                    //添加的提交请求
                    url = "/bus/customer/updateCustomer";
                }
            });
        }

        //删除删除客户
        function deleteCustomer(data) {
            //不存在则直接删除
            layer.confirm("确定要删除这条数据吗?",{"icon":3,"title":"提示"},function (index) {
                $.post("/bus/customer/deleteById",{"id":data.id},function (result) {
                    if(result.success){
                        //刷新数据表格
                        tableIns.reload();
                    }
                    //显示提示信息
                    layer.msg(result.message);
                },"json");
                //关闭弹框
                layer.close(index);
            })
        }




    });
</script>
</body>
</html>